<?php

/**
 * @name IndexController
 * @author 6p2b7gaqkyoe4aw\administrator
 * @desc 默认控制器
 * @see http://www.php.net/manual/en/class.yaf-controller-abstract.php
 */

error_reporting(E_ALL^E_NOTICE^E_WARNING);
class CategoryController extends Yaf_Controller_Abstract {

	public function init(){
	
		$arrConfig = Yaf_Application::app()->getConfig();
		$this->arrConfig=$arrConfig;
		$this->salt=$arrConfig->database->params->salt;
		$this->salt_time=$arrConfig->database->params->salt_time;
		//$salt=$arrConfig->database->params->salt;
	}
	
	public function checklogin1(){
		if(md5($this->salt.$_SESSION['user_token_time'].$_SESSION['user_id'])!=$_SESSION['user_token']){
			exit(json_encode(array('status'=>0,'info'=>'请先登陆后操作')));
		}
	}
	
	public function checklogin2(){
		$info=json_decode(file_get_contents("php://input"),true);
		$salt=$info['salt'] ? $info['salt'] : $_REQUEST['salt'];
		
		
		$where['salt= ?']=$salt;
		$usermodel=new UserModel();
		$user=$usermodel->getuser($where);
		if(!$user || time()-$user['salt_time']>$this->salt_time){
			exit(json_encode(array('status'=>0,'info'=>'登陆状态超时')));
		}
	}
	public function listAction(){
		
	}
	
	public function getAction(){
	
	}
	
	public function addAction(){
	
	}
	
	public function editAction(){
	
	}
	
	public function delAction(){
	
	}
	
	public function statusAction(){
	
	}
	/*
	 * 商品分类
	 */
	public function goods_categoryAction(){
		
		//$this->checklogin1();
		//$this->checklogin2();
		
		$info=json_decode(file_get_contents("php://input"),true);
		
		
		
		$sellerid=$info['sellerid'] ? $info['sellerid'] : $_REQUEST['sellerid'];
	
		//$catid=$_REQUEST['catid'] ? $_REQUEST['catid'] : '';
		$model = new CategoryModel();
	
	
		if($sellerid){
			$where['sellerid = ?']=$sellerid;
		}else{
			$where='';
		}
	
		$list = $model->get_goods_category($where);
		
		if($list) {
			$goodsmodel=new GoodsModel();
			
			foreach ($list as $key=>&$val){
					
				$goodswhere['goods_categoryid = ?']=$val['catid'];
				$goodswhere['sellerid = ?']=$sellerid;
				$val['goods']=$goodsmodel->getList2($goodswhere);
				//print_r($temp);
				unset($goodswhere);
					
			}
		}
		
		//print_r($list);
	echo json_encode($list);
	}
	
	/*
	 * 商家分类
	 */
	public function categoryListAction(){
		
		//$catid=$this->getRequest()->getPost('catid',false);
	
	    $info=json_decode(file_get_contents("php://input"),true);
		$catid=$info['catid'] ? $info['catid'] : $_REQUEST['catid'];
		
		//$catid=$_REQUEST['catid'] ? $_REQUEST['catid'] : '';
		$model = new CategoryModel();
		
		
		if($catid){
			$where['catid = ?']=$catid;
		}else{
			$where='';
		}
		
		$list = $model->getList($where);
		
		
		//print_r($list); 
		echo json_encode($list);
	}
	
}
